package com.cuanbo.dualscreenteachfu.activity;

import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cuanbo.dualscreenteachfu.R;
import com.cuanbo.dualscreenteachfu.base.App;
import com.cuanbo.dualscreenteachfu.base.LHBaseActivity;
import com.cuanbo.dualscreenteachfu.receiver.MyReceiver;
import com.cuanbo.dualscreenteachfu.utils.ActivityUtil;
import com.cuanbo.dualscreenteachfu.utils.BroadcastUtil;
import com.cuanbo.dualscreenteachfu.utils.DateUtil;
import com.cuanbo.dualscreenteachfu.utils.GpioUtils;
import com.cuanbo.dualscreenteachfu.utils.SharedPreferencesUtil;
import com.cuanbo.dualscreenteachfu.utils.TimerThread;
import com.cuanbo.dualscreenteachfu.utils.ToastUtil;
import com.cuanbo.dualscreenteachfu.widget.ColorSeekBar;

public class MainActivity extends LHBaseActivity implements  TimerThread.OnDoWorkListener, MyReceiver.MyReceiverListener {
    private ViewGroup rootView;
    private boolean whiteBoard=false;
    public static boolean comment=false;
    public static boolean recordState=false;
    public static int type=0;
    private TextView tvClass;
    private ColorSeekBar voicebar;
    private TimerThread timerThread;
    private TextView tvHHigh;
    private TextView tvHLow;
    private TextView tvMHigh;
    private TextView tvMLow;
    private RelativeLayout rlSignalNotebook;
    private RelativeLayout rlSignalTeacherDev;
    private RelativeLayout rlWirelessScreen;
    private RelativeLayout rlWhiteBoard;
    private ImageView ivWhiteBoard;
    private RelativeLayout rlComment;
    private ImageView ivComment;
    private MyReceiver myReceiver;
    public static int imgType=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rootView = (ViewGroup) LayoutInflater.from(this).inflate(R.layout.activity_main, null);
        setContentView(rootView);
        init();
    }

    private void init() {
        initView();
        GpioUtils.upgradeRootPermissionForExport();
        GpioUtils.windowsTouch();
        timerThread=new TimerThread(this,60000);
        registerReceiver(myReceiver=new MyReceiver(this),new IntentFilter("com.cuanbo.dralscreenteachfu.action"));

    }

    private void initView() {
        tvClass = rootView.findViewById(R.id.tvClass);
        voicebar = rootView.findViewById(R.id.voicebar);
        tvHHigh = rootView.findViewById(R.id.tvHHigh);
        tvHLow = rootView.findViewById(R.id.tvHLow);
        tvMHigh = rootView.findViewById(R.id.tvMHigh);
        tvMLow = rootView.findViewById(R.id.tvMLow);
        rlSignalNotebook = rootView.findViewById(R.id.rlSignalNotebook);
        rlSignalTeacherDev = rootView.findViewById(R.id.rlSignalTeacherDev);
        rlWirelessScreen = rootView.findViewById(R.id.rlWirelessScreen);
        rlWhiteBoard = rootView.findViewById(R.id.rlWhiteBoard);
        ivWhiteBoard = rootView.findViewById(R.id.ivWhiteBoard);
        rlComment = rootView.findViewById(R.id.rlComment);
        ivComment = rootView.findViewById(R.id.ivComment);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myReceiver);
    }

    /**
     *白板
     */
    public void btnWhiteBoard(View v) {
        rlComment.setBackgroundResource(R.drawable.btn_unck_bg);
        ivComment.setImageResource(R.drawable.icon_punctuate_screen2x);
        comment=false;
        whiteBoard=!whiteBoard;
        if(whiteBoard){
            GpioUtils.androidTouch();
            rlWhiteBoard.setBackgroundResource(R.drawable.btn_ck_bg);
            ivWhiteBoard.setImageResource(R.drawable.icon_whiteboard2x);
            type=1;
        }else{
            type=0;
            GpioUtils.windowsTouch();
            rlWhiteBoard.setBackgroundResource(R.drawable.btn_unck_bg);
            ivWhiteBoard.setImageResource(R.drawable.icon_whiteboard_screen2x);
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                BroadcastUtil.sendBroadcastToZhuScreen(MainActivity.type);
            }
        },2000);
    }

    /**
     *圈点
     */
    public void btnComment(View v) {
        rlWhiteBoard.setBackgroundResource(R.drawable.btn_unck_bg);
        ivWhiteBoard.setImageResource(R.drawable.icon_whiteboard_screen2x);
        whiteBoard=false;
        comment=!comment;
        if(comment){
            type=2;
            GpioUtils.androidTouch();
            rlComment.setBackgroundResource(R.drawable.btn_ck_bg);
            ivComment.setImageResource(R.drawable.icon_punctuate2x);
        }else{
            type=0;
            GpioUtils.windowsTouch();
            rlComment.setBackgroundResource(R.drawable.btn_unck_bg);
            ivComment.setImageResource(R.drawable.icon_punctuate_screen2x);
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                BroadcastUtil.sendBroadcastToZhuScreen(MainActivity.type);
            }
        },3000);

    }

    /**
    *上下课
    */
    public void btnClass(View v){
        App.configInfo.setClassRoomState(!App.configInfo.isClassRoomState());
        if(App.configInfo.isClassRoomState()){
            tvClass.setText("上课中");
            tvClass.setBackgroundResource(R.drawable.sk);
        }else{
            tvClass.setText("已下课");
            tvClass.setBackgroundResource(R.drawable.xk);
        }
        SharedPreferencesUtil.saveBean(this,App.configInfo);
    }

    @Override
    public void doWork() {
        tvHHigh.setText(""+DateUtil.getHourHigh());
        tvHLow.setText(""+DateUtil.getHourLow());
        tvMHigh.setText(""+DateUtil.getMinuteHigh());
        tvMLow.setText(""+DateUtil.getMinuteLow());
    }

    /**
     *笔记本
     */
    public void btnSignalNotebook(View v) {
        rlSignalNotebook.setBackgroundResource(R.drawable.signal_bg_selected2x);
        rlSignalTeacherDev.setBackgroundResource(R.drawable.signal_bg2x);
        rlWirelessScreen.setBackgroundResource(R.drawable.signal_bg2x);
        BroadcastUtil.sendBroadcastToZhuScreen(6);
    }

    /**
     *教师机
     */
    public void btnSignalTeacherDev(View v) {
        rlSignalNotebook.setBackgroundResource(R.drawable.signal_bg2x);
        rlSignalTeacherDev.setBackgroundResource(R.drawable.signal_bg_selected2x);
        rlWirelessScreen.setBackgroundResource(R.drawable.signal_bg2x);
        BroadcastUtil.sendBroadcastToZhuScreen(6);
    }

    /**
     *无线投屏
     */
    public void btnWirelessScreen(View v) {
        rlSignalNotebook.setBackgroundResource(R.drawable.signal_bg2x);
        rlSignalTeacherDev.setBackgroundResource(R.drawable.signal_bg2x);
        rlWirelessScreen.setBackgroundResource(R.drawable.signal_bg_selected2x);
        BroadcastUtil.sendBroadcastToZhuScreen(6);
    }

    /**
     *签到
     */
    public void btnSign(View v) {
        ActivityUtil.open(this,SignActivity.class);
        BroadcastUtil.sendBroadcastToZhuScreen(3);
    }

    /**
     *截屏
     */
    public void btnCapture(View v) {
        BroadcastUtil.sendBroadcastToZhuScreen(5);
        ToastUtil.show(this,"截屏中，请稍等");
    }

    /**
     *录屏
     */
    public void btnRecord(View v) {
        recordState=!recordState;
        if(recordState){
            ToastUtil.show(this,"开始录制");
            BroadcastUtil.sendBroadcastToZhuScreen(10);
        }else{
            ToastUtil.show(this,"停止录制");
            BroadcastUtil.sendBroadcastToZhuScreen(11);
        }
    }

    /**
     *黑屏
     */
    public void btnBlackScreen(View v) {

    }

    /**
     *放大镜
     */
    public void btnMagnifyingGlass(View v) {

    }

    /**
     *研讨教学
     */
    public void btnCompre(View v) {
        BroadcastUtil.sendBroadcastToZhuScreen(7);
    }

    /**
     *物联控制
     */
    public void btnCenterControl(View v) {
        ActivityUtil.open(this,DevControlActivity.class);
    }

    /**
     *录播控制
     */
    public void btnRecordControl(View v) {
        ActivityUtil.open(this,RecordControlActivity.class);
    }

    /**
     *一键对讲
     */
    public void btnTalkback(View v) {

    }

    /**
     *文件
     */
    public void btnFile(View v) {
        imgType=0;
        ActivityUtil.open(this,FileActivity.class);
    }

    @Override
    public void img1() {
        imgType=1;
        ActivityUtil.open(this,FileActivity.class);
    }

    @Override
    public void img2() {
        imgType=2;
        ActivityUtil.open(this,FileActivity.class);
    }
}